﻿using CarEdge.Infrastructure.Dto.Request.product;
using CarEdge.Infrastructure.Dto.Response.Product;
using CarEdge.Service.EFCore;
using CarEdge.Service.Interface;
using CarEdge.Service.Interface.Product;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CarEdge.Admin.Controllers.Product
{
    /// <summary>
    /// 产品
    /// </summary>
    /// 

    public class ProductController : BaseController
    {

        private readonly I_Product_EFCore _product_service;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="login_Service"></param>
        /// <param name="product"></param>
        public ProductController(I_Login_Service login_Service, I_Product_EFCore product) : base(login_Service)
        {
            _product_service = product;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadImage(IFormFileCollection file)
        {
            var result = await _product_service.UploadImage(file);
            return Ok(result);
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task<IActionResult> DeleteImage(IFormFileCollection file)
        {
            var result = await _product_service.DeleteImage(file);
            return Ok(result);
        }




        /// <summary>
        /// 获取所有产品
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_ProductAll(int? page = 1, int? limit = 10, string? key = "")
        {
            var result = await _product_service.Get_ProductAll(page ?? 1, limit ?? 10, key ?? "");
            return Ok(result);
        }

      /// <summary>
      /// 获取子级下拉框
      /// </summary>
      /// <param name="id"></param>
      /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_children_type(string id)
        {
            var result = await _product_service.Get_children_type(id);
            return Ok(result);
        }
        /// <summary>
        /// 获取子级并分页
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_children_Table(string id, int? page = 1, int? limit = 10, string? key = "")
        {
            var result = await _product_service.Get_children_Table (id, page ?? 1, limit ?? 10, key ?? "");
            return Ok(result);
        }

        /// <summary>
        /// 获取单个产品详情
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_Product(string id)
        {
            var result = await _product_service.Get_Product(id);
            return Ok(result);
        }
        /// <summary>
        ///批量/单个 删除产品
        /// </summary>
        /// <param name="pp">产品</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Delete_Product(Product_Response_Dto pp)
        {
            var result = await _product_service.Delete_Product(pp.ids);
            return Ok(result);
        }
        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="pp">产品</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Update_Product(Product_Response_Dto pp)
        {
            var result = await _product_service.Update_Product(pp);
            return Ok(result);
        }
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="pp">产品</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Add_Product(Product_Response_Dto pp)
        {
            var result = await _product_service.Add_Product(pp);
            return Ok(result);
        }
        /// <summary>
        /// 批量/单个 上/下架
        /// </summary>
        /// <param name="pp">只需要传你需要上/下架的产品ID就行</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Product_Islist(Product_Response_Dto pp)
        {
            var result = await _product_service.Product_Islist(pp.ids);
            return Ok(result);
        }
        /// <summary>
        /// 添加产品类型
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Add_Product_Type(Product_Type_Response_Dto pt)
        {
            var result = await _product_service.Add_Product_Type(pt);
            return Ok(result);
        }
        /// <summary>
        /// 获取所有产品类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_Product_Type()
        {
            var result = await _product_service.Get_Product_Type();
            return Ok(result);
        }
        /// <summary>
        /// 批量/单个 删除产品类型
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Delete_Product_Type(Product_Type_Response_Dto pt)
        {
            var result = await _product_service.Delete_Product_Type(pt.ids);
            return Ok(result);
        }
        /// <summary>
        /// 修改产品类型
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Update_Product_Type(Product_Type_Response_Dto pt)
        {
            var result = await _product_service.Update_Product_Type(pt);
            return Ok(result);
        }
        /// <summary>
        /// 获取单个产品类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_Product_Typeone(string id)
        {
            var result = await _product_service.Get_Product_Typeone(id);
            return Ok(result);
        }


        /// <summary>
        /// 添加套餐
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Add_Package(package_Request_Dto dto) { 
        
            var result=await _product_service.Add_Pacage(dto);
            return Ok(result);
        }
        /// <summary>
        /// 获取所有的套餐
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>

        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_All_Package(int? page = 1, int? limit = 10, string? key = "")
        {
            var result=await _product_service.Get_All_Package(page??1,limit??10,key??"");

            return Ok(result);
        }

        /// <summary>
        /// 获取单个套餐详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_Package_Detail(string id)
        {
            var result=await _product_service.Get_Package_Detail(id);
            return Ok(result);
        }
       /// <summary>
       /// 上架下级套餐
       /// </summary>
       /// <param name="dto"></param>
       /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> isActive_Package(package_Request_Dto dto)
        {
            var result =await _product_service.isActive_Package(dto);
            return Ok(result);
        }
        /// <summary>
        /// 修改套餐
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Update_Package(package_Request_Dto dto)
        {
            var result=await _product_service.Update_Package(dto);
            return Ok(result);
        }


        /// <summary>
        /// 获取类型树子级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_tree_package(string id)
        {
            var result =await _product_service.Get_tree_package(id);
            return Ok(result);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> Get_pro(Product_Response_Dto dto)
        {
            var result = await _product_service.Getpro(dto.ids);

            return Ok(result);
        }
    }
}
